package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.dto.supplier.ContractDto;
import com.yunyao.common.web.enums.ResultMsgEnum;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.*;
import com.yunyao.dao.service.bamu.IBamuContractNodePayService;
import com.yunyao.dao.service.bamu.IBamuContractWirePayService;
import com.yunyao.dao.service.bamu.IBamuPoSupplyMaterialService;
import com.yunyao.dao.service.bamu.IBamuPoSupplyService;
import com.yunyao.framework.dto.contract.ContractNodePaySaveDto;
import com.yunyao.framework.dto.contract.ContractWirePaySaveDto;

import com.yunyao.framework.dto.posyupply.PoSyupplyMaterialDeletedDto;
import com.yunyao.framework.dto.posyupply.PoSyupplyMaterialDto;
import com.yunyao.framework.dto.posyupply.PoSyupplyMaterialInfoSaveDto;
import com.yunyao.framework.dto.posyupply.PoSyupplyMaterialSaveDto;
import com.yunyao.framework.service.PoSyupplyOrderService;
import com.yunyao.framework.vo.contract.ContractNodePayVO;
import com.yunyao.framework.vo.contract.ContractWirePayVO;
;
import com.yunyao.framework.vo.posupply.PoSyupplyMaterialVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PoSyupplyOrderServiceImpl implements PoSyupplyOrderService {
    @Autowired
    private IBamuPoSupplyMaterialService iBamuPoSupplyMaterialService;
    @Autowired
    private IBamuPoSupplyService iBamuPoSupplyService;
    @Autowired
    private IBamuContractNodePayService iBamuContractNodePayService;

    @Autowired
    private IBamuContractWirePayService iBamuContractWirePayService;
    @Override
    public YResult<PageResult<PoSyupplyMaterialVO>> getSoSupplyMaterialList(PoSyupplyMaterialDto poSyupplyMaterialDto) {
        // 构建查询条件
        String contractId = poSyupplyMaterialDto.getContractId();
        String purchaseApplyId = poSyupplyMaterialDto.getPurchaseApplyId();
        String id = poSyupplyMaterialDto.getId();
        LambdaQueryWrapper<BamuPoSupplyMaterial> wrapper = new LambdaQueryWrapper<BamuPoSupplyMaterial>()
                .eq(StringUtils.isNotEmpty(purchaseApplyId), BamuPoSupplyMaterial::getPurchaseApplyId, purchaseApplyId)
                .eq(StringUtils.isNotEmpty(id), BamuPoSupplyMaterial::getSupplyId, id)
                .eq(BamuPoSupplyMaterial::getIsDeleted, DataDeletedStatus.NO.getType());
        // 分页查询
        IPage<BamuPoSupplyMaterial> payPage = new Page<>(poSyupplyMaterialDto.getPageIndex(), poSyupplyMaterialDto.getPageSize());
        payPage = iBamuPoSupplyMaterialService.page(payPage, wrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(payPage.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<PoSyupplyMaterialVO> materialVOList = materialDaoToVoList(payPage.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(materialVOList, payPage.getCurrent(), payPage.getSize(), payPage.getPages(), payPage.getTotal()));

    }

    @Override
    public YResult<PageResult<PoSyupplyMaterialVO>> getPoSupplyMaterialListbySupplyId(PoSyupplyMaterialDto poSyupplyMaterialDto) {

        LambdaQueryWrapper<BamuPoSupplyMaterial> wrapper = new LambdaQueryWrapper<BamuPoSupplyMaterial>()
                .in(BamuPoSupplyMaterial::getSupplyId, poSyupplyMaterialDto.getIdList()); // 使用in方法查询ID列表中的任意一个
//                .eq(BamuSoSupplyMaterial::getPurchaseApplyId, purchaseApplyId)
//                .eq(BamuPoSupplyMaterial::getIsDeleted, DataDeletedStatus.NO.getType());
        // 分页查询
        IPage<BamuPoSupplyMaterial> payPage = new Page<>(poSyupplyMaterialDto.getPageIndex(), poSyupplyMaterialDto.getPageSize());
        payPage = iBamuPoSupplyMaterialService.page(payPage, wrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(payPage.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<PoSyupplyMaterialVO> materialVOList = materialDaoToVoList(payPage.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(materialVOList, payPage.getCurrent(), payPage.getSize(), payPage.getPages(), payPage.getTotal()));


    }

    @Override
    public YResult<ContractWirePayVO> getWirePayInfo(ContractDto contractDto) {
        String contractId = contractDto.getContractId();
        LambdaQueryWrapper<BamuContractWirePay> wrapper = new LambdaQueryWrapper<BamuContractWirePay>()
                .eq(BamuContractWirePay::getContractId, contractId)
                .eq(BamuContractWirePay::getIsDeleted, DataDeletedStatus.NO.getType());
        BamuContractWirePay bamuContractWirePay = iBamuContractWirePayService.getOne(wrapper);
        if (Objects.isNull(bamuContractWirePay)) {
            return YResult.success();
        }
        ContractWirePayVO contractWirePayVO = wirePayDaoToVo(bamuContractWirePay);
        return YResult.success(contractWirePayVO);
    }

    private ContractWirePayVO wirePayDaoToVo(BamuContractWirePay bamuContractWirePay) {
        ContractWirePayVO contractWirePayVO = new ContractWirePayVO();
        BeanUtils.copyProperties(bamuContractWirePay, contractWirePayVO);
        return contractWirePayVO;
    }

    @Override
    public YResult<PageResult<ContractNodePayVO>> getContractNodePayList(ContractDto contractDto) {
        // 构建查询条件
        String contractId = contractDto.getContractId();
        LambdaQueryWrapper<BamuContractNodePay> wrapper = new LambdaQueryWrapper<BamuContractNodePay>()
                .eq(BamuContractNodePay::getContractId, contractId)
                .eq(BamuContractNodePay::getIsDeleted, DataDeletedStatus.NO.getType());
        // 分页查询
        IPage<BamuContractNodePay> payPage = new Page<>(contractDto.getPageIndex(), contractDto.getPageSize());
        payPage = iBamuContractNodePayService.page(payPage, wrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(payPage.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<ContractNodePayVO> nodePayVOList = nodePayDaoToVoList(payPage.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(nodePayVOList, payPage.getCurrent(), payPage.getSize(), payPage.getPages(), payPage.getTotal()));
    }

    @Override
    public YResult<String> saveMaterialInfo(PoSyupplyMaterialSaveDto saveDto) {
        List<PoSyupplyMaterialInfoSaveDto> dtoList = saveDto.getSaveDtoList();
        if (CollectionUtils.isEmpty(dtoList)) {
            return YResult.error(ResultMsgEnum.PARAM_ERROR);
        }
        List<BamuPoSupplyMaterial> daoList = materialDtoToDaoList(dtoList);
        if (CollectionUtils.isEmpty(daoList)) {
            return YResult.success();
        }
        iBamuPoSupplyMaterialService.saveOrUpdateBatch(daoList, 20);
        return YResult.success(saveDto.getContractId());
    }

    private List<BamuPoSupplyMaterial> materialDtoToDaoList(List<PoSyupplyMaterialInfoSaveDto> dtoList) {
        return dtoList.stream().map(this::materialDtoToDao).collect(Collectors.toList());
    }

    private BamuPoSupplyMaterial materialDtoToDao(PoSyupplyMaterialInfoSaveDto poSyupplyMaterialInfoSaveDto) {
        BamuPoSupplyMaterial bamuPoSupplyMaterial = new BamuPoSupplyMaterial();
        BeanUtils.copyProperties(poSyupplyMaterialInfoSaveDto, bamuPoSupplyMaterial);
        return bamuPoSupplyMaterial;
    }

    @Override
    public YResult<String> savePayInfo(ContractWirePaySaveDto saveDto) {

        BamuContractWirePay contractWirePay = new BamuContractWirePay();
        BeanUtils.copyProperties(saveDto, contractWirePay);
        iBamuContractWirePayService.saveOrUpdate(contractWirePay);

        List<BamuContractNodePay> nodePayDtoToDaoList = nodePayDtoToDaoList(saveDto.getNodePaySaveDtoList());
        if (CollectionUtils.isNotEmpty(nodePayDtoToDaoList)) {
            iBamuContractNodePayService.saveBatch(nodePayDtoToDaoList);
        }
        return YResult.success(saveDto.getContractId());
    }

    @Override
    public YResult<String> deletedMaterialInfo(PoSyupplyMaterialDeletedDto deletedDto) {
        if (CollectionUtils.isEmpty(deletedDto.getIdList())) {
            return YResult.error(ResultMsgEnum.PARAM_ERROR);
        }
        List<BamuPoSupplyMaterial> updateList = Lists.newArrayList();
        for (String id : deletedDto.getIdList()) {
            BamuPoSupplyMaterial poSupplyMaterial = new BamuPoSupplyMaterial();
            poSupplyMaterial.setId(id);
            poSupplyMaterial.setIsDeleted(DataDeletedStatus.YES.getType());
            updateList.add(poSupplyMaterial);
        }
        iBamuPoSupplyMaterialService.updateBatchById(updateList, 20);
        return YResult.success(deletedDto.getContractId());
    }

    private List<BamuContractNodePay> nodePayDtoToDaoList(List<ContractNodePaySaveDto> nodePaySaveDtoList) {
        if (CollectionUtils.isEmpty(nodePaySaveDtoList)) {
            return Lists.newArrayList();
        }
        return nodePaySaveDtoList.stream().map(this::nodePayDtoToDao).collect(Collectors.toList());
    }

    private BamuContractNodePay nodePayDtoToDao(ContractNodePaySaveDto contractNodePaySaveDto) {
        BamuContractNodePay bamuContractNodePay = new BamuContractNodePay();
        BeanUtils.copyProperties(contractNodePaySaveDto, bamuContractNodePay);
        return bamuContractNodePay;
    }

    private List<ContractNodePayVO> nodePayDaoToVoList(List<BamuContractNodePay> records) {
        if (CollectionUtils.isEmpty(records)) {
            return Lists.newArrayList();
        }
        return records.stream().map(this::nodePayDaoToVo).collect(Collectors.toList());
    }

    private ContractNodePayVO nodePayDaoToVo(BamuContractNodePay bamuContractNodePay) {
        ContractNodePayVO contractNodePayVO = new ContractNodePayVO();
        BeanUtils.copyProperties(bamuContractNodePay, contractNodePayVO);
        return contractNodePayVO;
    }

    private List<PoSyupplyMaterialVO> materialDaoToVoList(List<BamuPoSupplyMaterial> records) {
        return records.stream().map(this::materialDaoToVo).collect(Collectors.toList());
    }

    private PoSyupplyMaterialVO materialDaoToVo(BamuPoSupplyMaterial bamuPoSupplyMaterial) {
        PoSyupplyMaterialVO poSyupplyMaterialVO = new PoSyupplyMaterialVO();
        LambdaQueryWrapper<BamuPoSupply> wrapper = new LambdaQueryWrapper<BamuPoSupply>()
                .eq(BamuPoSupply::getId, bamuPoSupplyMaterial.getSupplyId());
        BamuPoSupply bamuPoSupply = iBamuPoSupplyService.getOne(wrapper);
        BeanUtils.copyProperties(bamuPoSupplyMaterial, poSyupplyMaterialVO);
        poSyupplyMaterialVO.setOrderSn(bamuPoSupply.getOrderSn());
        return poSyupplyMaterialVO;
    }
}
